const { defineConfig } = require('@vue/cli-service')
function resolve(dir) {
  return path.join(__dirname, dir)
}

const webpack = require('webpack')
//添加copy-webpack-plugin
const CopyWebpackPlugin = require('copy-webpack-plugin')
//引入path
const path = require('path')
module.exports = defineConfig({
  transpileDependencies: true,
  lintOnSave: false,
  configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    resolve: {
      alias: {
        '@': resolve('src')
      },
      fallback: {
        http: require.resolve("stream-http"),
        https: require.resolve("https-browserify"),
        zlib: require.resolve("browserify-zlib"),

        // assert: require.resolve("assert/"),
        stream: require.resolve("stream-browserify"),
      }
    },
    plugins: [
      // Copy Cesium Assets, Widgets, and Workers to a static directory
      new CopyWebpackPlugin({
        patterns: [{
          from: "node_modules/cesium/Build/Cesium/Workers",
          to: "Workers"
        },
        {
          from: "node_modules/cesium/Build/Cesium/ThirdParty",
          to: "ThirdParty",
        },
        {
          from: "node_modules/cesium/Build/Cesium/Assets",
          to: "Assets"
        },
        {
          from: "node_modules/cesium/Build/Cesium/Widgets",
          to: "Widgets"
        },
        ],
      }),
      new webpack.DefinePlugin({
        // Define relative base path in cesium for loading assets
        CESIUM_BASE_URL: JSON.stringify(""),
      }),
 
      // new BundleAnalyzerPlugin() //解开可查看项目打包分布情况
    ],
    module: {
      // Removes these errors: "Critical dependency: require function is used in a way in which dependencies cannot be statically extracted"
      // https://github.com/AnalyticalGraphicsInc/cesium-webpack-example/issues/6
      unknownContextCritical: false,
      unknownContextRegExp: /\/cesium\/cesium\/Source\/Core\/buildModuleUrl\.js/,
      rules: [
        {
          test: /\.mjs$/,
          include: /node_modules/,
          type: "javascript/auto"
        },
      ]
    }
  }
  // chainWebpack: (config) => {
  //   config.module.rule("svg").exclude.add(resolve("src/icons")).end();
  //   config.module
  //     .rule("icons")
  //     .test(/\.svg$/)
  //     .include.add(resolve("src/icons"))
  //     .end()
  //     .use("svg-sprite-loader")
  //     .loader("svg-sprite-loader")
  //     .options({
  //       symbolId: "icon-[name]",
  //     })
  //     .end();
  // }

  // webpack原生配置
  // configureWebpack: {
  //   build: {
  //     // emplate for index.html
  //     index: path.resolve(__dirname, '../dist/index.html'),
  //     // Paths
  //     assetsRoot: path.resolve(__dirname, '../dist'),
  //     assetsSubDirectory: 'static',
  //     assetsPublicPath: './',
      
  //     productionSourceMap: false,
  //     // https://webpack.js.org/configuration/devtool/#production
  //     devtool: '#source-map',

  //     // Gzip off by default as many popular static hosts such as
  //     // Surge or Netlify already gzip all static assets for you.
  //     // Before setting to `true`, make sure to:
  //     // npm install --save
  //     // -dev compression-webpack-plugin
  //     productionGzip: false,
  //     productionGzipExtensions: ['js', 'css'],

  //     // Run the build command with an extra argument to
  //     // View the bundle analyzer report after build finishes:
  //     // `npm run build --report`
  //     // Set to `true` or `false` to always turn it on or off
  //     bundleAnalyzerReport: process.env.npm_config_report
  //   },
  //   module: {
  //     rules: [
  //       {
  //         test: /\.worker\.js$/,
  //         use: {
  //           loader: 'worker-loader',
  //           options: {
  //             inline: true,
  //           },
  //         },
  //       },
  //     ],
  //   },
  // },
  ,
  devServer: {
    proxy: {
      '/music': {
        target: `http://openapi.music.163.com`,
        changeOrigin: true,
        pathRewrite: {
          '/music': ''
        }
      }
    },
    // disableHostCheck: true
  },

})
